<?php
namespace app\supply;


use app\common\Common;


class Supply extends Base{
    //供货商列表
    public function supply_business($page=1,$pre=5,$order = "id",$desc=""){
        $b = ($page-1)*$pre;
        $business = Common::db_count("id as shop_id,user_id,proxy_name,address,portrait_phone,portrait_img,verify_status","","drh_proxy_shop_info","verify_status=1  ORDER BY ".$order." ".$desc."  limit ".$b.",".$pre);
//        foreach ($business as $k=>$v){
//           $business[$k]['goods']=Common::db_count("Channel_commodity_id as goods_id","","drh_proxy_classification","account_id=".$v['id']);
//           foreach ( $business[$k]['goods'] as $key=>$val){
//               if($val !== ""){
//                   if($val['goods_id'] == ""){
//                       unset($business[$k]['goods'][$key]);
//                   }else{
//                       foreach ($business[$k]['goods'][$key] as $ke => $value){
//                           $business[$k]['goods'][$key] = implode(',',array_filter(explode(',',$value)));
//                       }
//                   }
//               }
//           }
//           // $business[$k]['goods'] = Common::er_array($business[$k]['goods']);
//        }

//        foreach ($business as $k=>$v){
//            if($v['verify_status'] !== 1){
//                unset($business[$k]);
//            }
//        }
        return json([
            'code'=>$this->t_code,
            'msg'=>"ok",
            'data'=> [
                "list"=>$business,
                'total'=> Common::db_count_id("COUNT(id)","count","drh_proxy_shop_info","verify_status=1 "),
            ]]);
    }
    //供货商商品列表
    public function supply_goods(int $shop_id,$page=1,$pre=5,$order ="id",$desc=0){
        $classif_id = Common::db_count("user_id","","drh_proxy_shop_info","id={$shop_id}")[0]['user_id']??-1;

        $goods_assortment = Common::db_count("id,Channel_commodity_id","","drh_proxy_classification","account_id=".$classif_id);

        $data= array();
        foreach ($goods_assortment as $k=>$v){
                if($v['Channel_commodity_id'] !== ""){
                    $data[]= explode(',',$v['Channel_commodity_id']);
                }
        }

        $date = implode(",",array_filter(Common::er_array($data)));

        if($date ==""){
            $date = -1;
        }
        $b = ($page-1)*$pre;
        if($desc == 0){
            $good = Common::db_count("id,rebates,lightspot,goods_img,buying_price,shop_id,goods_name,goods_price,give_integral,goods_group,goods_freight,goods_hot,goods_new,platform_lv,proxy_goods_id,proxy_goods_value,goods_stock,goods_info,goods_img,goods_attr_value","","drh_goods"," is_ware=1 AND is_shelves=1 AND id IN(".$date.")  ORDER BY {$order}  limit {$b},{$pre}");
        }else{
            $good = Common::db_count("id,rebates,lightspot,goods_img,buying_price,shop_id,goods_name,goods_price,give_integral,goods_group,goods_freight,goods_hot,goods_new,platform_lv,proxy_goods_id,proxy_goods_value,goods_stock,goods_info,goods_img,goods_attr_value","","drh_goods"," is_ware=1 AND is_shelves=1 AND id IN(".$date.")  ORDER BY {$order} desc limit {$b},{$pre}");
        }
        $repeat = Common::db_count("goods_id","","drh_goods_proxy","goods_id IN(".$date.")");
        foreach ($good as $k=>$v){
            if(!empty($repeat[$k]['goods_id']) == $v['id']){
                $good[$k]['repeat']= 1;
            }else{
                $good[$k]['repeat']= 0;
            }
            $good[$k]['goods_img'] = unserialize($v['goods_img']);
            $good[$k]['goods_attr_value'] = unserialize($v['goods_attr_value']);
        }
        return json([
            'code'=>$this->t_code,
            'msg'=>"ok",
            'data'=>[
                'list'=>$good,
                "total"=>Common::db_count_id("COUNT(id)","count","drh_goods"," is_ware=1 AND id IN(".$date.") ")
            ]
        ]);
    }
    //商户代理商品
    public function agent(int $shop_id,int $goods_id,int $proportion,int $proxy_shop_id,int $goods_type_id){
            $repeat = Common::db_count("COUNT(id)","`count`","drh_goods_proxy","shop_id=".$shop_id." AND goods_id=".$goods_id." and is_not=0 ")[0]["count"]??0;
            if($repeat>=1){
                return json(['code'=>$this->f_code,"msg"=>"您已代理该商品"]);
            }
            $data = [
                'shop_id'=>$shop_id,
                'goods_id'=>$goods_id,
                'proportion'=>$proportion,
                'proxy_shop_id'=>$proxy_shop_id,
                'goods_type_id'=>$goods_type_id,
                'create_time'=>time()
            ];
            $agent = Common::db_insert("drh_goods_proxy",$data);
            if($agent){
                return json(['code'=>$this->t_code,"msg"=>"ok"]);
            }
                return json(['code'=>$this->t_code,"msg"=>"代理无效"]);
    }
    //代理供货商商品列表
    public function commodity($shop_id){
        $shop  = implode(",",Common::er_array(Common::db_count("goods_id","","drh_goods_proxy","shop_id=".$shop_id." and is_not=0")));
        $ware = Common::db_count("*","","drh_goods","is_ware=1 AND id IN(".$shop.")");
        foreach ($ware as $key=>$val){
            $ware[$key]["goods_attr_value"] = unserialize($val['goods_attr_value']);
            $ware[$key]["goods_img"] = unserialize($val['goods_img']);
        }
        return json(['code'=>$this->t_code,"msg"=>"ok","data"=>$ware]);
    }
    //商户取消代理或修改分类
    public function canceltd(int $shop_id,int $goods_id,$type_id = ""){
        if($type_id == ""){
            Common::db_upload("drh_goods_proxy","is_not=1 WHERE shop_id={$shop_id} AND goods_id={$goods_id}");
            return json(['code'=>$this->t_code,"msg"=>"ok"]);
        }else{
            Common::db_upload("drh_goods_proxy","goods_type_id={$type_id} WHERE shop_id={$shop_id} AND goods_id={$goods_id}");
            return json(['code'=>$this->t_code,"msg"=>"ok"]);
        }
    }
}